package com.zhw.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhw.mapper.CustomerMapper;
import com.zhw.pojo.dto.CustomerLossQueryDTO;
import com.zhw.pojo.entry.Customer;
import com.zhw.pojo.dto.CustomerQueryDTO;
import com.zhw.pojo.PageBean;
import com.zhw.pojo.entry.CustomerLoss;
import com.zhw.pojo.entry.CustomerReprieve;
import com.zhw.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerMapper customerMapper;

    @Override
    public PageBean page(CustomerQueryDTO customerQueryDTO) {
        PageHelper.startPage(customerQueryDTO.getPage(), customerQueryDTO.getPageSize());
        Page<Customer> page = customerMapper.page(customerQueryDTO);
        return new PageBean(page.getTotal(), page.getResult());

    }


    @Override
    public List<Map<String, String>> getAddresses() {
        List<String> list = customerMapper.getAddress();

        List<Map<String, String>> result = new ArrayList<>();
        for (String s : list) {
            if (s != null && s.length() != 0) {
                Map m = new HashMap();
                m.put("label", s);
                m.put("value", s);
                result.add(m);
            }

        }
        return result;
    }

    @Override
    public void save(Customer customer) {
        customer.setUpdateDate(LocalDateTime.now());
        customer.setCreateDate(LocalDateTime.now());
        customer.setStatus(1);
        customer.setIsValid(1);
        customer.setKhno("KH" + System.currentTimeMillis());
        customer.setXyd(getStar(Integer.parseInt(customer.getXyd())));
        customerMapper.save(customer);

    }


    public String getStar(Integer number) {
        String[] stars = {"待考察", "☆", "☆☆", "☆☆☆", "☆☆☆☆", "☆☆☆☆☆"};
        return stars[number];
    }


    @Override
    public Customer getByID(Integer id) {
        Customer c = customerMapper.getByID(id);
        if(c.getXyd()!=null&&c.getXyd().length()>0)c.setXyd(String.valueOf(getStartCount(c.getXyd())));
        return c;
    }

    public Integer getStartCount(String s) {
        for (int i = 0; i < s.length(); i++) {
            if (s.contains("☆")) {
                return s.length();
            }
        }
        return 0;
    }

    @Override
    public void update(Customer c) {
        c.setUpdateDate(LocalDateTime.now());
        c.setXyd(getStar(Integer.parseInt(c.getXyd())));
        customerMapper.update(c);
    }

    @Override
    public void delete(Integer id) {
        customerMapper.delete(id);
    }

    @Override
    public List<Map<String, String>> getCustomerName() {
        List<Map<String, String>> result = new ArrayList<>();
        List<Customer> cs = customerMapper.getName();

        for (Customer c : cs) {
            if (c != null) {
                Map<String, String> map = new HashMap<>();
                map.put("label", c.getName());
                map.put("value", String.valueOf(c.getId()));
                result.add(map);

            }

        }


        return result;

    }


    @Override
    public PageBean getLossPage(CustomerLossQueryDTO customerLossQueryDTO) {
        PageHelper.startPage(customerLossQueryDTO.getPage(),customerLossQueryDTO.getPageSize());
        Page<CustomerLoss> page=customerMapper.getLossPage(customerLossQueryDTO);
        return new PageBean(page.getTotal(),page.getResult());

    }


    @Override
    public CustomerLoss getLossByID(Integer id) {
        return customerMapper.getLossByID(id);
    }


    @Override
    public List<CustomerReprieve> getReprieveByLossID(Integer id) {
        return customerMapper.getReprieveByLossID(id);
    }


    @Override
    public void add(CustomerReprieve customerReprieve) {
        customerReprieve.setCreateDate(LocalDateTime.now());
        customerReprieve.setUpdateDate(LocalDateTime.now());
        customerMapper.addReprieve(customerReprieve);
    }

    @Override
    public void checkLoss(Integer id) {
        CustomerLoss customerLoss=new CustomerLoss();
        customerLoss.setId(id);
        customerLoss.setUpdateDate(LocalDateTime.now());
        customerLoss.setIsValid(1);
        customerLoss.setStatus(1);
        customerLoss.setConfirmLossTime(LocalDateTime.now());

        customerMapper.updateLoss(customerLoss);

    }


    @Override
    public CustomerReprieve getReprieveByID(Integer id) {
        return  customerMapper.getReprieveByID(id);
    }


    @Override
    public void updateReprieve(CustomerReprieve customerReprieve) {
        customerReprieve.setUpdateDate(LocalDateTime.now());
        customerMapper.updateReprieve(customerReprieve);
    }


    @Override
    public void deleteReprieveByID(Integer id) {
        customerMapper.deleteReprieveByID(id);
    }
}
